IF OBJECT_ID('ssf.log_end') IS NOT NULL
	DROP PROCEDURE ssf.log_end
GO

CREATE PROCEDURE ssf.log_end
	@log_id BIGINT
	, @row_count BIGINT = NULL
	, @log_error_number INT = NULL
	, @log_error_line INT = NULL
	, @log_error_message NVARCHAR(2048) = NULL
AS
/*

*/

SET NOCOUNT ON

-- ============
/*<code_comment><![CDATA[declare local variables]]></code_comment>*/
DECLARE	@sql_error INT
			, @row_count_update INT
-- ============

-- ============
/*<code_comment><![CDATA[update log record as complete]]></code_comment>*/
UPDATE ssf.log_info WITH (ROWLOCK)
SET end_spid = @@SPID
	, end_login = SUSER_SNAME()
	, end_log_time = SYSDATETIME()
	, row_count = @row_count
	, log_error_number = @log_error_number
	, log_error_line = @log_error_line
	, log_error_message = NULLIF(@log_error_message, '')
WHERE log_id = @log_id

SELECT @sql_error = @@ERROR
	, @row_count_update = @@ROWCOUNT

IF @sql_error <> 0
OR @row_count_update <> 1
BEGIN
	RAISERROR('The UPDATE to the table <ssf.log_info> failed.', 16, 1)
	RETURN @sql_error
END
-- ============

RETURN @@ERROR
GO
